/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets.customer;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import pojo.Buy;
import pojo.Cart;
import services.ServiceCart;
import services.ServiceHistory;

/**
 *
 * @author khalifaabdelaziz
 */
public class ServletViewHistory extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         ServiceHistory serviceHistory = new ServiceHistory();
    List<Buy> buys;
       PrintWriter out = response.getWriter();

        HttpSession session = request.getSession();
        String userIdsession = (String) session.getAttribute("userId");
        int userId = Integer.parseInt(userIdsession);

        buys = serviceHistory.viewHistory(userId);
        //carts = serviceCart.findCartByUserId(4);

        out.println("<tr><th>Name</th> <th>Price</th><th>Quantity</th> <th>Subtotal</th> </tr>");

        for (Buy buy : buys) {

            String name = buy.getProductId().getName();
            int price = buy.getProductId().getPrice();
            int quantity = buy.getAmount();
            int subtotal = price * quantity;

            out.println("<tr><td>" + name + "</td> <td>" + price + "</td> <td>" + quantity + "</td> <td>" + subtotal + "</td> </tr>");

        }
    }

}
